import { ListCategoryParm } from "@/api/category/CategoryModel";
import { onMounted, reactive, ref, nextTick } from "vue";
import { getListApi } from "@/api/category";

export default function useTable() {
   //表格高度
   const tableHeight = ref(0)
   //列表查询的参数
   const listParm = reactive<ListCategoryParm>({
      categoryName: '',
      page: 1,
      pageSize: 10,
      totalSize: 0,
   })
   //表格数据
   const tableList = ref([])
   //列表查询
   const getList = async () => {
      let result = await getListApi(listParm)
      if (result && result.code == 200) {
         listParm.totalSize = result.data.totalPage;
         tableList.value = result.data.data;
      }
   }
   //搜索
   const searchBtn = () => {
      getList()
   }
   //重置
   const resetBtn = () => {
      listParm.page = 1
      listParm.categoryName = ''
      getList()
   }
   //总页数改变时触发
   const sizeChange = (size: number) => {
      listParm.pageSize = size;
      getList()
   }
   //当前页数改变时触发
   const currentChange = (page: number) => {
      listParm.page = page;
      getList()
   }
   onMounted(() => {
      getList()
      //计算表格高度
      nextTick(()=>{
         tableHeight.value = window.innerHeight -435
     })
   })
   return {
      listParm,
      getList,
      searchBtn,
      resetBtn,
      tableList,
      sizeChange,
      currentChange,
      tableHeight,
   }
}